package com.situ.vivamarket.api.sys;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.situ.vivamarket.model.Member;
import com.situ.vivamarket.model.Order;
import com.situ.vivamarket.model.search.MemberSearchBean;
import com.situ.vivamarket.service.MemberService;
import com.situ.vivamarket.service.OrderService;
import com.situ.vivamarket.util.JsonResult;
import com.situ.vivamarket.util.MemAddGroup;
import com.situ.vivamarket.util.MemEditGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/api/v1/member")
public class MemberApi {
    private MemberService memberService;
    private OrderService orderService;

    @Autowired
    public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
    }
    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    @GetMapping
    public ResponseEntity<JsonResult> list(MemberSearchBean msb,
                                           @RequestParam (defaultValue = "1") Integer page,
                                           @RequestParam (defaultValue = "10")Integer limit) {
            Page<?> p= new Page<>(page, limit);//分页对象
            List<Member> members = memberService.getAllMembers(msb,p);
            PageInfo<Member> pi = new PageInfo<>(members);
            JsonResult result=JsonResult.successData(members);
            result.setCount((int)pi.getTotal());
            return ResponseEntity.ok(result);

    }

    @DeleteMapping
    public ResponseEntity<JsonResult> delete(Integer ids[]) {
        if(ids.length==0){
            return ResponseEntity.noContent().build();
        }else{
            for(Integer id:ids){
                List<Order> orders= orderService.findByMemberId(id);
                if(orders.size()>0){
                    return ResponseEntity.ok(JsonResult.fail("该会员有订单，无法删除"));
                }
            }
            int count=memberService.deleteByIds(ids);
            if(count>0){
                return ResponseEntity.ok(JsonResult.successData(count));
            }else{
                return ResponseEntity.ok(JsonResult.fail("删除会员失败"));
            }
        }
    }
    @PostMapping
    public ResponseEntity<JsonResult> add(@Validated(MemAddGroup.class)  Member member) {
//       使用validated进行参数校验，校验失败，会抛出异常
            boolean success=memberService.add(member);
            return success ? ResponseEntity.ok(JsonResult.successData(member)) : ResponseEntity.internalServerError().build();
    }
    @PutMapping
    public ResponseEntity<JsonResult> update(@Validated(MemEditGroup.class) Member member) {
        //       使用validated进行参数校验，校验失败，会抛出异常
        boolean success=memberService.update(member);
        return success ? ResponseEntity.ok(JsonResult.successData(member)) : ResponseEntity.internalServerError().build();
    }
}
